package com.aplikata.evok.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.aplikata.evok.bo.ResultDetail;
import com.aplikata.service.PublicService;
import com.aplikata.util.ServiceFinder;
import com.aplikata.util.StringUtil;

/** 
* @ClassName: ResultConverter 
* @Description: TODO
* @author Jasenko
* @date 17.5. 2012. 09:30:00 
*  
*/
@FacesConverter( value="resultDetailConverter" )
public class ResultDetailConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if (StringUtil.isStrEmpty(arg2))
			return null;
		
		PublicService ps = (PublicService) ServiceFinder.findBeanForWeb("publicService");
		return ps.get(ResultDetail.class, Long.valueOf(arg2));
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if (arg2 == null)
			return null;

		if(arg2 instanceof String && StringUtil.isStrEmpty(arg2.toString()))
			return null;
		
		return ((ResultDetail)arg2).getId().toString();
	}
}
